package qmm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import qmm.dao.CartDao;
import qmm.dao.UserDao;
import qmm.dao.impl.CartDaoImpl;
import qmm.dao.impl.UserDaoImpl;
import qmm.entity.User;
import qmm.entity.vo.CartVo;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet{
	UserDao userDao = new UserDaoImpl();

	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();//获取session
		//获取表单数据
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String auto = request.getParameter("auto");
		
		User user = userDao.login(account, password);
		if(user!=null){
			if("on".equals(auto)){
				//创建cookie对象
				Cookie cookie1 = new Cookie("account",account);
				Cookie cookie2 = new Cookie("password",password);
				//写入cookie
				response.addCookie(cookie1);
				response.addCookie(cookie2);
			}	
			CartDao cartDao = new CartDaoImpl();
			List<CartVo> list = cartDao.findCartVo(user.getId());
			session.setAttribute("cartVoList", list);//用户购物车列表
			session.setAttribute("user", user);//用户信息存入session
			session.removeAttribute("msg");//删除错误消息
			response.sendRedirect("index.jsp");
		}else{
			System.out.println("用户名或密码错误");
			session.setAttribute("msg", "用户名或密码错误");
			response.sendRedirect("login.jsp");
		}
	}
	
	public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//获取表单数据
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String recommend = request.getParameter("recommend");
		String recommendId = request.getParameter("recommendId");
		String code = request.getParameter("code");
		
		int n = userDao.add(account, password, email, phone);
		if(n>0){
			session.removeAttribute("registMsg");//注册成功删除错误消息
			response.sendRedirect("login.jsp");
		}else{
			session.setAttribute("registMsg", "注册失败");
			response.sendRedirect("regist.jsp");
		}
	}
	
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie:cookies){
			if(cookie.getName().equals("account")){
				cookie.setMaxAge(0);//失效
				response.addCookie(cookie);
			}
			if(cookie.getName().equals("password")){
				cookie.setMaxAge(0);//失效
				response.addCookie(cookie);
			}
		}
		
		HttpSession session = request.getSession();//获取session
		session.removeAttribute("user");//删除用户信息
		response.sendRedirect("index.jsp");
	}
	
	public void checkAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//设置请求字符集
		response.setCharacterEncoding("utf-8");//设置响应字符集
		String account = request.getParameter("account");
		int n = userDao.checkAccount(account);
		if(n>0){
			response.getWriter().write("用户名重复，请换一个");//返回文本
		}			
	}
	
	public void checkCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//设置请求字符集
		response.setCharacterEncoding("utf-8");//设置响应字符集
		String card = request.getParameter("card");
		int n = userDao.checkCard(card);
		if(n>0){
			response.getWriter().write("身份证号重复，请换一个");//返回文本
		}		
	}
	
	public void checkEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//设置请求字符集
		response.setCharacterEncoding("utf-8");//设置响应字符集
		String email = request.getParameter("email");
		int n = userDao.checkEmail(email);
		if(n>0){
			response.getWriter().write("邮箱重复，请换一个");//返回文本
		}	
	}
	
	public void checkPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//设置请求字符集
		response.setCharacterEncoding("utf-8");//设置响应字符集
		String phone = request.getParameter("phone");
		int n = userDao.checkPhone(phone);
		if(n>0){
			response.getWriter().write("手机号重复，请换一个");//返回文本
		}	
	}
	
	public void checkPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String password = request.getParameter("password");
		int n = userDao.checkPwd(password, user.getId());
		if(n>0){
			response.getWriter().write("success");
		}
	}
	
	public void editCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String card = request.getParameter("card");
		int n = userDao.editCard(card, user.getId());
		if(n>0){
			user.setIdCardNo(card);
			session.setAttribute("user", user);//刷新session数据
		}
	}
	
	public void editEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String email = request.getParameter("email");
		int n = userDao.editEmail(email, user.getId());
		if(n>0){
			user.setEmail(email);
			session.setAttribute("user", user);//刷新session数据
		}
	}
	
	public void editNickName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String nickname = request.getParameter("nickname");
		int n = userDao.editNickName(nickname, user.getId());
		if(n>0){
			user.setNickname(nickname);
			session.setAttribute("user", user);//刷新session数据
		}
	}
	
	public void editPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String password = request.getParameter("newPassword");
		userDao.editPassword(password, user.getId());
		response.sendRedirect("LogoutServlet");
	}
	
	public void editPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String phone = request.getParameter("phone");
		int n = userDao.editPhone(phone, user.getId());
		if(n>0){
			user.setPhone(phone);
			session.setAttribute("user", user);//刷新session数据
		}
	}
}
